package com.tywj.buscustomerapp.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CSActivity extends BaseActivity {
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.head_left) ImageView back;
    @BindView(R.id.line1) LinearLayout line1;
    @BindView(R.id.line2) LinearLayout line2;
    @BindView(R.id.line3) LinearLayout line3;
    @BindView(R.id.cs_edit) EditText editText;
    @BindView(R.id.cs_submit) TextView submit;

    @Override
    protected int getContentId() {
        return R.layout.activity_cs;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText(R.string.drawer_cs);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        line1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("拨打咨询电话", Config.CALL_NUMBER);
            }
        });
        line2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("拨打投诉电话",Config.CALL_NUMBER);
            }
        });
        line3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CSActivity.this,ProblemActivity.class));
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String content = editText.getText().toString();
                if (content.equals("")){
                    ToastJoe.getmToastJoe().ToastLongShow(CSActivity.this,null,"您的建议不能为空");
                }else{
                    Observable.create(new ObservableOnSubscribe() {
                        @Override
                        public void subscribe(ObservableEmitter e){
                            try {
                                String msg = WebServicrUtils.getInstance()
                                        .postForBack(SPUtils.getUserPhone(getApplicationContext()),content, TimeRender.getFullDateStr1());
                                if (msg.equals("ok")){
                                    e.onNext(msg);
                                }else{
                                    e.onError(null);
                                }
                            } catch (IOException e1) {
                                e1.printStackTrace();
                                e.onError(null);
                            } catch (XmlPullParserException e1) {
                                e1.printStackTrace();
                                e.onError(null);
                            }
                        }
                    }).subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer() {
                                @Override
                                public void onSubscribe(Disposable d) {
                                    ToastJoe.getmToastJoe().ToastShowRight(CSActivity.this,null,"正在提交");
                                }

                                @Override
                                public void onNext(Object o) {
                                    ToastJoe.getmToastJoe().ToastShowRight(CSActivity.this,null,"反馈成功");
                                    editText.setText("");
                                }


                                @Override
                                public void onError(Throwable e) {
                                    ToastJoe.getmToastJoe().ToastLongShow(CSActivity.this,null,"反馈失败，请重试");
                                }

                                @Override
                                public void onComplete() {

                                }
                            });
                }
            }
        });
    }

    private void showDialog(final String title, final String msg){
        final AlertDialog build = new AlertDialog.Builder(CSActivity.this,R.style.AlertDialogStyle).create();
        //自定义布局
        View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        build.setView(view, 0, 0, 0, 0);
        build.show();
        //得到当前显示设备的宽度，单位是像素
        int width = getWindowManager().getDefaultDisplay().getWidth();
        //得到这个dialog界面的参数对象
        WindowManager.LayoutParams params = build.getWindow().getAttributes();
        //设置dialog的界面宽度
        params.width = width-(width/3);
        //设置dialog高度为包裹内容
        params.height =  WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的重心
        params.gravity = Gravity.CENTER;
        build.getWindow().setAttributes(params);
        TextView titleTv = view.findViewById(R.id.message);
        TextView messageTv = view.findViewById(R.id.message1);
        titleTv.setText(title);
        messageTv.setText(msg);
        Button btn_sure = view.findViewById(R.id.yes);
        Button btn_cancel = view.findViewById(R.id.no);
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+msg));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                build.dismiss();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                build.dismiss();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("客服");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("客服");
        MobclickAgent.onPause(this);
    }
}
